package com.dtagri.fjwlw.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.View;

import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.viewholders.MonitoringAlarmViewHolder;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjAlarmData;
import com.dtagri.fjwlw.bean.TjAreaMsg;

/**
 * @author wxjn
 * 
 */
public class MonitoringAlarmAdapter extends
		CommonAdapter<TjAreaMsg, MonitoringAlarmViewHolder> {

	public MonitoringAlarmAdapter(Context context, List<TjAreaMsg> mDataSet) {
		super(context, mDataSet);
	}

	@Override
	public int getCount() {
		int i = 0;
		for (TjAreaMsg tjAreaMsg : mDataSet) {
			List<SmallArea> smallAreas = tjAreaMsg.getSmallAreas();
			for (SmallArea smallArea : smallAreas) {
				List<TjAlarmData> tjAlarmDatas = smallArea.getTjAlarmDatas();
				i += tjAlarmDatas.size();
			}
		}
		return i;
	}

	/**
	 * 格式化时间
	 * 
	 * @param alarmTime
	 * @return
	 */
	private String format(String alarmTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy HH:mma",
				Locale.ENGLISH);
		Date parse = null;
		try {
			parse = sdf.parse(alarmTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.ENGLISH);
		String format = sdf2.format(parse);
		return format;
	}

	@Override
	protected MonitoringAlarmViewHolder createViewHolder() {
		return new MonitoringAlarmViewHolder();
	}

	@Override
	protected void setItemData(int position,
			MonitoringAlarmViewHolder viewHolder, View rootView) {

		// 遍历树得到当前大区\小区\传感器
		int i = 0;
		for (TjAreaMsg tjAreaMsg : mDataSet) {
			List<SmallArea> smallAreas = tjAreaMsg.getSmallAreas();
			for (SmallArea smallArea : smallAreas) {
				List<TjAlarmData> tjAlarmDatas = smallArea.getTjAlarmDatas();
				int j = 0;
				for (TjAlarmData tjAlarmData : tjAlarmDatas) {
					if (position == i) {
						// curRealTimeData = vRealTimeData;
						viewHolder.tvArea.setText(tjAreaMsg.getAreaName()
								+ " / " + smallArea.getSmallName());
						viewHolder.tvKey.setText(tjAlarmData.getEquipName());
						if (tjAlarmData.getType() == 1) {
							viewHolder.tvValue.setText(tjAlarmData.getValue()
									+ tjAlarmData.getUnit() + " ↑");
						} else {
							viewHolder.tvValue.setText(tjAlarmData.getValue()
									+ tjAlarmData.getUnit() + " ↓");
						}
						// 等级 4:红色、3:橙色 2:黄色、1蓝色
						switch (tjAlarmData.getRank()) {
						case 4:
							viewHolder.tvValue.setTextColor(mContext
									.getResources().getColor(R.color.red));
							break;
						case 3:
							viewHolder.tvValue.setTextColor(mContext
									.getResources().getColor(R.color.orange));
							break;
						case 2:
							viewHolder.tvValue.setTextColor(mContext
									.getResources().getColor(R.color.yellow));
							break;
						case 1:
							viewHolder.tvValue.setTextColor(mContext
									.getResources().getColor(R.color.blue));
							break;
						}
						String alarmTime = tjAlarmData.getAlarmTime();
						String format = format(alarmTime);
						viewHolder.tvTime.setText(format);
						if (j == 0) {
							viewHolder.titleLine.setVisibility(View.VISIBLE);
						} else {
							viewHolder.titleLine.setVisibility(View.GONE);
						}
					}
					i++;
					j++;
				}
			}
		}

	}

}
